package com.ad_hccm.controller.GeneralInformation;

import com.ad_hccm.controller.Code;
import com.ad_hccm.controller.Result;
import com.ad_hccm.domain.GeneralInformation.GeneralInformation;
import com.ad_hccm.exception.BusinessException;
import com.ad_hccm.service.GeneralInformation.GeneralInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO 一般资料Controller
 */
@RestController
@RequestMapping("/general_information")
public class GeneralInformationController {

    @Autowired
    GeneralInformationService generalInformationService;

    @PostMapping
    public Result save(@RequestBody GeneralInformation generalInformation){
        boolean flag = generalInformationService.save(generalInformation);
        String msg = flag ? "添加失败" : "添加成功" ;
        return new Result( flag ? Code.SAVE_OK : Code.SAVE_ERR, flag, msg);
    }

    @PutMapping
    public Result update(@RequestBody GeneralInformation generalInformation) {
        boolean flag = generalInformationService.update(generalInformation);
        String msg = flag ? "修改成功" : "数据修改失败，请重试";
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR , flag , msg);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable String id) {
        if(id.length()!=18) throw new BusinessException(Code.GET_ERR, "请输入18位的身份证");
        GeneralInformation generalInformation = generalInformationService.getById(id);
        Integer code = generalInformation != null ? Code.GET_OK : Code.GET_ERR;
        String msg = generalInformation != null ? "查询成功" : "数据查询失败，请重试";
        return new Result(code , generalInformation , msg);
    }

    @GetMapping
    public Result getAll() {
        List<GeneralInformation> generalInformationList = generalInformationService.getAll();
        Integer code = generalInformationList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = generalInformationList != null ? "" : "数据查询失败，请重试";
        return new Result(code , generalInformationList , msg);
    }

}
